﻿$(function () {

    getAnnouncements();
    getNotification();

    $("#txtDate").datepicker();

});


function getAnnouncements() {
    $('#AnnouncementTemp').empty();
    ajaxCall('../DataService.asmx/GetAnnouncements', false, "",
            function (msg) {
                if (msg.d == null) {
                    $('#AnnouncementTemp').html("Announcements were not found");
                } 
                else {
                  for (i in msg.d) {
                        var announcements = [
                            { ID: msg.d[i].ID, Title: msg.d[i].Title, Date: JSONDate(msg.d[i].Date), Description: msg.d[i].Description },
                        ];
                        $('#AnnouncementTemplate').tmpl(announcements).appendTo('#AnnouncementTemp');
                  }
            }
            },
            function () {
                alert("Error happend while reading from database");
            }
        );
}

function getNotification() {
    $('#notTemp').empty();
    ajaxCall('../DataService.asmx/GetNotifications', false, "",
            function (msg) {
                if (msg.d == null) {
                    $('#notTemp').html("Notifications were not found");
                } 
                else {
                    for (i in msg.d) {
                        var notifications = [
                            { ID: msg.d[i].ID, Title: msg.d[i].Title, Date: JSONDate(msg.d[i].Date), Description: msg.d[i].Description, Link: msg.d[i].Link },
                        ];
                        $('#NotificationTemplate').tmpl(notifications).appendTo('#notTemp');
                    }   
                }
            },
            function () {
                alert("Error happend while reading from database");
            }
        );
}

function DeleteAnn(id) {
    var data;
    if (confirm("Are you sure that you want to delete this item?")) {
        data = "{'ID':'" + id + "'}";
        ajaxCall('../DataService.asmx/DeleteAnnouncement', false, data,
                function () {
                    alert("Deleting process was successful!");
                    getAnnouncements();
                },
                function () {
                    alert("Error happend while deleting this item");
                }
            );
    }
}

function openDialog() {
    $("#insertNotification").dialog({
        modal: true,
        closeOnEscape: false
    });
}


function closeDialog() {
    $("#txtTitle").val("");
    $("#txtDate").val("");
    $("#linkToPage").val("");
    $("#txtDescription").val("");
    $("#insertNotification").dialog("close");
}


function DeleteNotification(id) {
    if (confirm("Are you sure that you want to delete this item?")) {
        var data = '{"ID": "' + id + '"}';
        ajaxCall('../DataService.asmx/DeleteNotification', false, data,
                function () {
                    alert("Item deleted succesfully!");
                    getNotification();
                },
                function () {
                    alert("Error happend while deleting this item");
                }
            );
    }
}

function insertNotification() {
    $('#validateNotification').validate({
        rules: {
            notificationtitle: "required",
            notificationdate: "required",
            notiflinktopage: {
                required:true,
                
            },
            notificationdesc: "required"

        },
        messages: {
            notificationtitle: "Title is required",
            notificationdate: "Date is required",
            notiflinktopage: {
                required: "Link is required",
               
            },
            notificationdesc: "Short description is required"
        },

        errorPlacement: function (error, element) {
            error.insertBefore(element);
        }

    });

    if ($("#txtTitle").valid() && $("#txtDate").valid() && $("#linkToPage").valid() && $("#txtDescription").valid()) {
        var dataToSend = "{'Title': '" + $("#txtTitle").val() + "', 'Date': '" + $("#txtDate").val() + "' , 'Description' : '" + $("#txtDescription").val() + "', 'Link' : '" + $("#linkToPage").val() + "'}";
        ajaxCall('../DataService.asmx/InsertNotification', false, dataToSend,
                function () {
                    $("#txtTitle").val("");
                    $("#txtDate").val("");
                    $("#linkToPage").val("");
                    $("#txtDescription").val("");
                    closeDialog();
                    getNotification();
                },
                function () {
                    alert("Error happend while inserting data to database");
                }
            );

    }
}